/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var preorderTraversal = function(root) {
    // 需要一个栈 用栈来模拟遍历数据 从根节点开始遍历 将根节点插入栈 然后出栈 将出栈的左右子节点插入到栈中 先右后左 这样能够使得进入结果数组的顺序是先做后右
    let stack = []
    let res = []
    if(root = null) return res
    stack.push(root)
    while(stack.length){
      let node = stack.pop()
      res.push(node.val)
      node.left && stack.push(node.left)
      node.right && stack.push(node.right)
    }
    return res
};